﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//***
//注入方法1：
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = false)]
//***


namespace DataCapture.Helper
{
    /// <summary>
    /// 消息日志
    /// </summary>
    public class MessageLog
    {
        public int logLevel { get; set; }
        public string message { get; set; }
    }

    public enum LogLevel
    {
        Trace = 0,
        Debug = 1,
        Info = 2,
        Warn = 3,
        Error = 4,
        Fatal = 5
    }

    public static class Log4NetHelper
    {
        /***
        //注入方法2:
        static Log4NetHelper()
        {
            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            string path = Path.Combine(baseDirectory, "log4net.config");
            XmlConfigurator.ConfigureAndWatch(new FileInfo(path));
        }
        ***/

        private static readonly ILog LInfo = LogManager.GetLogger("LogInfo");
        private static readonly ILog LError = LogManager.GetLogger("LogError");
        private static readonly ILog LMonitor = LogManager.GetLogger("LogMonitor");
        private static readonly ILog LDebug = LogManager.GetLogger("LogDebug");
        private static readonly ILog LWarn = LogManager.GetLogger("LogWarn");
        private static readonly ILog LFatal = LogManager.GetLogger("LogFatal");

        public static void Trace(string mesStr, LogLevel level)
        {
            MessageLog message = new MessageLog()
            {
                logLevel = (int)level,
                message = mesStr
            };
            EventBusHelper.EventBus().PublishAsync(new RWEventCommon<MessageLog>("TRACE_LOG", message)); // 通知到消息系统
        }

        /// <summary>
        /// 记录Error日志
        /// </summary>
        /// <param name="errorMsg"></param>
        /// <param name="trace"></param>
        /// <param name="ex"></param>
        public static void Error(string errorMsg, bool trace = false, Exception ex = null)
        {
            if (trace)
            {
                Trace(errorMsg, LogLevel.Error);
            }
            if (ex != null)
            {
                LError.Error(errorMsg, ex);
            }
            else
            {
                LError.Error(errorMsg);
            }
        }

        /// <summary>
        /// 记录Info日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="trace"></param>
        /// <param name="ex"></param>
        public static void Info(string msg, bool trace = false, Exception ex = null)
        {
            if (trace)
            {
                Trace(msg, LogLevel.Info);
            }
            if (ex != null)
            {
                LInfo.Info(msg, ex);
            }
            else
            {
                LInfo.Info(msg);
            }
        }

        /// <summary>
        /// 记录Monitor监视器日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="trace"></param>
        /// <param name="ex"></param>
        public static void Monitor(string msg, bool trace = false, Exception ex = null)
        {
            if (trace)
            {
                Trace(msg, LogLevel.Info);
            }
            LMonitor.Info(msg);
        }

        /// <summary>
        /// 记录Debug日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="trace"></param>
        /// <param name="ex"></param>
        public static void Debug(string msg, bool trace = false, Exception ex = null)
        {
            if (trace)
            {
                Trace(msg, LogLevel.Debug);
            }
            if (ex != null)
            {
                LDebug.Debug(msg, ex);
            }
            else
            {
                LDebug.Debug(msg);
            }
        }

        /// <summary>
        /// 记录Warn日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="trace"></param>
        /// <param name="ex"></param>
        public static void Warn(string msg, bool trace = false, Exception ex = null)
        {
            if (trace)
            {
                Trace(msg, LogLevel.Warn);
            }
            if (ex != null)
            {
                LWarn.Warn(msg, ex);
            }
            else
            {
                LWarn.Warn(msg);
            }
        }

        /// <summary>
        /// 记录Fatal日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="trace"></param>
        /// <param name="ex"></param>
        public static void Fatal(string msg, bool trace = false, Exception ex = null)
        {
            if (trace)
            {
                Trace(msg, LogLevel.Fatal);
            }
            if (ex != null)
            {
                LFatal.Fatal(msg, ex);
            }
            else
            {
                LFatal.Fatal(msg);
            }
        }
    }
}
